// component/filterHouseCooperation/filterHouseCooperation.js
import * as transferData from './../../models/transferData'
import api from './../../common/api'

Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的传入样式
   */
  externalClasses: ['style-extra'],

  /**
   * 组件的属性列表
   */
  properties: {
    isCustomHeader: {
      type: String,
      value: '0',
    },
    history: {
      type: String,
      value: '',
    },
    accurate: {
      type: String,
      value: '',
    },
    placeholder: {
      type: String,
      value: '搜索关键字',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentCity: '',
    searchCurrent: '',
    districtCurrent: [],
    isShowFilterDistrict: false,
    pickerSelect: [0, 0],
    pickerList: [
      [{
        label: '区域',
        value: 'district',
      }],
      []
    ],
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
      t.ininData()
      t.loadCityDistrictList()
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 页面的生命周期
   */
  pageLifetimes: {
    show: function () {
      let t = this
      t.checkSearchKeyword()
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    ininData() {
      let t = this
      t.myData = {
        _pickerList: [],
      }
    },

    onSearchClose() {
      let t = this
      t.setData({
        searchCurrent: '',
      })
      t.triggerEvent('searchKeywordChange', {
        id: '',
        keyword: '',
        type: ''
      })
    },

    onSearchJump() {
      let t = this
      let {
        history,
        accurate,
        placeholder,
        currentCity,
        searchCurrent
      } = t.data
      wx.navigateTo({
        url: `/pages/cooperation/search/search?history=${history}&accurate=${accurate}&placeholder=${placeholder}&city=${currentCity}&search=${searchCurrent}`,
      })
    },

    onDistrictDropdown() {
      let t = this
      t.setData({
        isShowFilterDistrict: true
      })
      t.loadCityDistrictList()
    },

    checkSearchKeyword() {
      let t = this
      let key = transferData.getData('cooperativeSearch')
      transferData.resetData('cooperativeSearch')
      if (key) {
        t.setData({
          searchCurrent: key.label,
        })
        t.triggerEvent('searchKeywordChange', key)
      }
    },

    onPickerConfirm(e) {
      let t = this
      let {
        select,
        result
      } = e.detail || {}
      let districtCurrent = result && result.length ? result.map(item => {
        return item.label
      }) : []
      let area = result && result.length ? result.map(item => {
        return item.value
      }) : []
      t.setData({
        districtCurrent,
        pickerSelect: select,
      })
      t.triggerEvent('filterAreaChange', area)
    },

    onPickerClose() {
      let t = this
      t.setData({
        isShowFilterDistrict: false
      })
    },

    loadCityDistrictList() {
      let t = this
      if (t.myData._pickerList && t.myData._pickerList.length) {
        t.setData({
          pickerList: t.myData._pickerList,
        });
      } else {
        wx.getStorage({
          key: "currentCity",
          success(res) {
            let cityCode = res.data.cityCode;
            t.setData({
              currentCity: res.data.city || '',
            });
            api.getAreaList({}, cityCode, (data) => {
              let list = data.data.content || []
              t.data.pickerList[1] = list.map(item => {
                return {
                  label: item.areaName || '',
                  value: item.areaName || '',
                  code: item.areaCode || '',
                  id: item.areaId || ''
                }
              })
              t.data.pickerList[1].unshift({
                label: '不限',
                value: ''
              })
              t.myData._pickerList = t.data.pickerList
              t.setData({
                pickerList: t.data.pickerList,
              });
            });
          },
        });
      }
    },

  }

})